<?php
require_once("../system/funciones_bd.php");
require_once("../system/sesion.class.php");
//Conexion base de datos local
$link = mysql_connect("localhost", "root", "");
$sesion = new sesion();
$usuario = $sesion->get("usuario");
$usuarioId = $sesion->get("idusuario");
if ($usuario == false) {
    header("Location: ../index.php");
} else {
    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("sirpof_db1", $link);
    mysql_set_charset('utf8', $link);
    ?>
    <!DOCTYPE html>
    <html lang="de"><!-- use class="debug" here if you develope a template and want to check-->
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0" />
            <!-- some meta tags, important for SEO"--> 
            <meta name="description" content="put a short description in here" />
            <meta name="keywords" content="put your important keywords in here" />
            <meta name="revisit-after" content="7 days" />
            <meta name="robots" content="index,follow" />

            <title>ForensicTooth v1.0</title>


            <link rel="stylesheet" href="../css/inuit.css" />
            <link rel="stylesheet" href="../css/fluid-grid16-1100px.css" />
            <link rel="stylesheet" href="../css/eve-styles.css" />
            <link rel="shortcut icon" href="../icon.png" />
            <link rel="apple-touch-icon-precomposed" href="../img/icon.png" />
            <link rel="stylesheet" href="../css/jquery-ui.css" />
            <script src="../js/jquery-1.9.1.js"></script>
            <script src="../js/jquery-ui.js"></script>            
            <script src="../js/respond-min.js" type="text/javascript"></script>
        </script>
        <script>
            $(function() {
                $("#dialog").dialog({
                    autoOpen: false,
                    modal: true
                });
                $("#abrir")
                        .button()
                        .click(function() {
                            $("#dialog").dialog("open");
                        });
            });
        </script>
        <!--local fallback for JQuery-->

    </head>

    <!--===============================================================  Logo, social and menu =====================================================================================--> 
    <body>
        <div class="wrapper">	
            <a href="../index.php" id="logo"><img src="../img/logo.png" alt="something" />
                <h1 class="accessibility"></h1></a>
            <div class="social">
                <h2>
                <?php echo $sesion->get("usuario"); ?> 
                </h2>
            </div>


            <ul id="nav" class="main">
                <li><a href="#" class="active">Crear Paciente</a></li>
                <li><a href="principalForence.php">Regresar</a></li>
                <li><a href="../system/cerrarsesion.php">salir</a></li>
            </ul>

        </div><!--end of wrapper div--> 

        <div class="clear"></div>    
        <!--===============================================================  Intro =====================================================================================--> 
        <div class="wrapper">
            <div class="grids">
                <?php
                if (isset($_POST['btnGuardar'])) {
                    $activo = $_POST["activo"];
                    $necropsia = $_POST["necropsia"];
                    $autoriza = $_POST["autoriza"];
                    $nombre = $_POST["nombre"];
                    $sapellido = $_POST["apellido"];
                    $tdocumento = $_POST["tdocumento"];
                    $ndocumento = $_POST["ndocumento"];
                    $genero = $_POST["genero"];
                    $fnacimiento = $_POST["fnacimiento"];
                    $lnacimiento = $_POST["lnacimiento"];
                    $edadDesde = $_POST["edad"];
                    $edadHasta = $_POST["edadhasta"];
                    $idusuario = $_POST["idusuario"];
                    $fecharegistro = date("Y-m-d H:i:s");
                    if ($genero == '1') {
                        $foto = "imagen.png";
                    } else IF ($genero == '0') {
                        $foto = "imagen1.png";
                    }

                    $sqlproducto = "INSERT INTO paciente_postmorten VALUES (NULL, '" . $idusuario . "','" . $nombre . "','" . $sapellido . "','" . $tdocumento . "','" . $ndocumento . "','" . $genero . "','" . $fnacimiento . "','" . $lnacimiento . "','" . $edadDesde . "', '" . $edadHasta . "' ,'" . $fecharegistro . "','" . $foto . "','" . $activo . "', '" . $necropsia . "', '" . $autoriza . "');";
                    //echo $sqlproducto;
                    mysql_query($sqlproducto, $link);
                    //echo $sqlproducto;

                    $sqlodontograma = "insert into paciente_odontograma_post
                                        SELECT DISTINCT NULL, '$fecharegistro',(SELECT max(id_pacientepost) FROM paciente_postmorten) as id_paciente, 1 FROM paciente_postmorten ";
                    //echo $sqlodontograma;
                    mysql_query($sqlodontograma, $link);
                    $sqlodontogramaDetalle = "insert into odontogramapost
                                        SELECT NULL , idlista,  '#FFFFFF',  '#FFFFFF',  '#FFFFFF',  '#FFFFFF',  '#FFFFFF',  (
                                            SELECT MAX( id_registro ) FROM paciente_odontograma_post
                                        ) AS id_registro, 1
                                        FROM lista WHERE id_grupo = 1";
                    //echo $sqlodontogramaDetalle;
                    mysql_query($sqlodontogramaDetalle, $link);
                    header("Location: principalForence.php");
                }
                //Finalmente hacemos la insercion en otra tabla
                ?>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <table>
                        <thead>
                            <tr>
                                <th scope="col" colspan="2"><h2>PACIENTE POST MORTEM</h2></th>
                        </tr>
                        </thead>
                        <tfoot>
                            <tr><td colspan="5">&nbsp;</td></tr>
                        </tfoot>
                        <tbody>
                            <tr>
                                <th scope="col" colspan="2"><input type="checkbox" name="activo" checked>Habilitado</input></th>
                            </tr>
                            <tr>
                                <th scope="col">*Necropsia</th>
                                <?php
                                $sqlConsecutivo = "SELECT (MAX(necropsia)+1) AS MAXIMO FROM paciente_postmorten";
                                $resul = mysql_query($sqlConsecutivo, $link);
                                $resultado = mysql_fetch_array($resul);
                                $necropsia = $resultado[0];
                                if ($necropsia == NULL) {
                                    $nnecropsia = 1;
                                } else {
                                    $nnecropsia = $resultado[0];
                                }
                                ?>
                                <th scope="col"><input type="text" value="<?php echo $nnecropsia; ?>" class="input" id="pnombre" name="necropsia" onFocus="blur()"></th>
                            </tr>
                            <tr>
                                <th scope="col">*Autoridad Solicitante</th>
                                <th scope="col"><input type="text" class="input" id="snombre" name="autoriza" maxlength="50" /></th>
                            </tr>
                            <tr>
                                <th scope="col">*Nombre</th>
                                <th scope="col"><input type="text" class="input" id="papellido" name="nombre" maxlength="20" required/></th>
                            </tr>
                            <tr>
                                <th scope="col">Apellido</th>
                                <th scope="col"><input type="text" class="input" id="sapellido" name="apellido" maxlength="20"/></th>
                            </tr>
                            <tr>
                                <th scope="col">*Genero</th>
                                <th scope="col">
                                    <select name="genero" class="input" required> 
                                        <option value="">Seleccione...</option>
                                        <option value="1">Masculino</option>
                                        <option value="0">Femenino</option>
                                    </select></th>
                            </tr>
                            <tr>
                                <th scope="col">*Tipo de Documento</th>
                                <th scope="col">
                                    <select name="tdocumento" class="input" required> 
                                        <option value="null">Seleccione...</option>
                                        <option value="1">Registro Civil</option>
                                        <option value="2">Tarjeta de Identidad</option>
                                        <option value="3">Cedula de Ciudadania</option>
                                        <option value="4">Cedula de Extranjeria</option>
                                        <option value="5">Indefinido</option>
                                    </select>
                                </th>
                            </tr>
                            <tr>
                                <th scope="col">Número Documento</th>
                                <th scope="col"><input type="text" class="input" id="ndocumento" name="ndocumento" maxlength="10" /></th>
                            </tr>
                            <tr>
                                <th scope="col">Fecha Nacimiento</th>
                                <th scope="col"><input type="date" class="input" name="fnacimiento"  /></th>
                            </tr>
                            <tr>
                                <th scope="col">*Lugar Nacimiento</th>
                                <th scope="col"><input type="text" class="input" id="lnacimiento" name="lnacimiento" maxlength="120" required /></th>
                            </tr>
                            <tr>
                                <th scope="col">*Edad</th>
                                <th scope="col">Desde:<input type="text" class="input" id="edad" name="edad" maxlength="120" required />
                                    <br />Hasta:<input type="text" class="input" id="lnacimiento" name="edadhasta" maxlength="120" required />
                                </th>
                            </tr>
                        <input name="idusuario" type="hidden" class="input" value="<?php echo $usuarioId ?> " required> 
                        <tr>
                            <th scope="col" colspan="2">
                                <input type="submit" value="Guardar" name="btnGuardar" class="button" />
                                <input type="reset" value="Limpiar" name="btnLimpiar" class="button" />
                            </th>
                        </tr>

                        </tbody>
                    </table>

                </form>


            </div><!--end of div grids-->

        </div><!--end of div wrapper-->    

        <!--===============================================================  Footer =====================================================================================-->  
        <div class="wrapper">
            <div id="footer">


                <div class="grids">
                    <center>
                        <p>ForensicTooth v 1.0</p>
                    </center>

                </div><!--end of grids-->
            </div><!--end of footer-->
        </div><!--end of wrapper-->



    </body>
    </html>

    <?php
}
?>